<?php
	$this->breadcrumbs = array(
		'Каталог'=>array('/katalog'),
		'Корзина'
		);

    $rate = Config::model()->find(array('condition'=>'code=:code', 'params'=>array(':code'=>'rate')))->value + 0;

    $this->setPageTitle('Ваша корзина');
?>

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'category-form',
    'action'=>'/Order/default/createOrder',
    // Please note: When you enable ajax validation, make sure the corresponding
    // controller action is handling ajax validation correctly.
    // There is a call to performAjaxValidation() commented in generated controller code.
    // See class documentation of CActiveForm for details on this.
    'enableAjaxValidation'=>true,
    'errorMessageCssClass'=>'alert alert-danger',
    'htmlOptions'=>array('enctype'=>'multipart/form-data')
)); ?>


        <h1>
            <?php echo $this->getPageTitle(); ?>
        </h1>

    <?php if ($products): ?>
    <table class="table table-hover table-striped" style="font-size: 12px;">
        <thead>
        <tr>
            <th>Название</th>
            <th>Цена</th>
            <th>Количество</th>
            <th>Общая стоимость</th>
            <th></th>
        </tr>
        </thead>
        <tbody>
        <?php $total_price = 0; ?>
        <?php foreach($products as $product):?>
    <tr>
        <td><?php echo $product->title; ?></td>
        <td style="min-width: 70px;"><?php echo (is_numeric($product->price)?number_format($product->price,0,'',' ')."":""); ?></td>
        <td >
            <div class="input-group input-group-sm" style="width: 110px;">
                <span class="input-group-btn">
                    <button id="<?php echo 'minus'.$product->id?>" class="btn btn-default" type="button"><span class="glyphicon glyphicon-minus"></span></button>
                </span>
                <?php echo CHtml::textField('prod_count'. $product->id, $cart[$product->id], array('submit'=>"/dobavit_v_korzinu/", 'params'=>array('id'=>$product->id), 'size'=>1, 'class'=>'form-control', 'style'=>'text-align: center; padding: 0;')); ?>
                <span class="input-group-btn">
                    <button id="<?php echo 'plus'.$product->id?>" class="btn btn-default" type="button"><span class="glyphicon glyphicon-plus"></span></button>
                </span>
            </div>
        </td>
        <td><?php $total_price += ($cart[$product->id])*($product->price); echo number_format(($cart[$product->id])*($product->price),0,'',' '); ?></td>
        <td style='width: 60px;'>
            <?php echo CHtml::link('<span class="glyphicon glyphicon-remove"></span>', "#", array(
                'submit'=>array('/Category/cart/removeFromCart'),
                'params'=>array('id'=>$product->id),
                'class' => 'btn-sm btn-danger',
                'confirm' => 'Вы действительно хотите удалить из корзины товар "'.$product->title.'"?',
                'csrf' => true)); ?>
        </td>
    </tr>
        <script type="text/javascript" >
            $(document).ready(function() {
                $("<?php echo '#minus'.$product->id?>").click(function () {
                    var $input = $(this).parent().parent().find('input');
                    var count = parseInt($input.val()) - 1;
                    count = count < 1 ? 1 : count;
                    $input.val(count);
                    $input.change();
                    return false;
                });
                $("<?php echo '#plus'.$product->id?>").click(function () {
                    var $input = $(this).parent().parent().find('input');
                    $input.val(parseInt($input.val()) + 1);
                    $input.change();
                    return false;
                });
            });
        </script>
            <?php endforeach; ?>
        <tr>
            <td>Итого: </td>
            <td></td>
            <td></td>
            <td><b><?php echo number_format($total_price,0,'',' '); ?> б. р.<br/> (<?php echo number_format($total_price/$rate,0,'',' '); ?> у.е.)</b></td>
            <td></td>
        </tr>
</tbody>
</table>
    <div class="block">
        <?php echo CHtml::link('Продолжить покупки', '/katalog', array('class' => 'btn btn-default')); ?>
        <?php echo CHtml::submitButton('Оформить заказ', array('class' => 'btn btn-info')); ?>
    </div>

<?php else: ?>
    <div class="block">
        Ваша корзина пуста
    </div>

    <div class="block">
        <?php echo CHtml::link('Приступить к покупкам', '/katalog', array('class' => 'btn btn-default')); ?>
    </div>
<?php endif; ?>
<?php $this->endWidget(); ?>

